import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/article_item.dart';
import 'package:flutter_app/compents/state_page.dart';
import 'package:flutter_app/controller/project/project_controller.dart';
import 'package:flutter_app/route/routes_page.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ProjectContentPage extends StatefulWidget {
  final String authorId;

  ProjectContentPage(this.authorId);

  @override
  _ProjectContentPageState createState() => _ProjectContentPageState();
}

class _ProjectContentPageState extends State<ProjectContentPage>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return GetX<ProjectController>(
      //Get.find<>()这个东西就是用来获取p层(数据层的数据类似于创建啦一个单例)，
      // 但是首先需要使用Get.put(XMLFilesStatController())或Get.lazyPut(()=>XMLFilesStatController())。
      // 然后你可以通过调用来创建你的对象var xmlCtrl = Get.find<XMLFilesStatController>();。
      // 如果您希望您的XMLFilesStatController对象是永久的，即您只想创建一次对象并将其用于整个应用程序生命周期，
      // 那么您需要这样做Get.put(XMLFilesStatController(),permanent=true)
      init:
          Get.put<ProjectController>(ProjectController(), tag: widget.authorId),
      initState: (_) {
        var controller = Get.find<ProjectController>(tag: widget.authorId);
        controller.setCid(widget.authorId);
        controller.initData(true);
      },
      builder: (_) {
        return StatePageWithViewController<ProjectController>(
          model: Get.find<ProjectController>(tag: widget.authorId),
          controller: Get.find<ProjectController>(tag: widget.authorId)
              .refreshController,
          onPressed: () {
            Get.find<ProjectController>(tag: widget.authorId).initData(true);
          },
          onRefresh: () async {
            Get.find<ProjectController>(tag: widget.authorId).refresh();
          },
          onLoading: () async {
            Get.find<ProjectController>(tag: widget.authorId)
                .getProjectList(false);
          },
          child: _buildListUI(
              Get.find<ProjectController>(tag: widget.authorId).articleItems),
        );
      },
    );
  }

  Widget _buildListUI(List articleItems) {
    return ListView.builder(
      itemBuilder: (context, index) {
        return _buildListItemUI(articleItems[index]);
      },
      itemCount: articleItems.length,
    );
  }

  Widget _buildListItemUI(ArticleItem articleItem) {
    return Container(
      padding: EdgeInsets.only(top: 15, right: 10, bottom: 5, left: 10),
      child: InkWell(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            CachedNetworkImage(
              imageUrl: articleItem.envelopePic,
              fit: BoxFit.fill,
              width: 80.w,
              height: 120.h,
            ),
            SizedBox(width: 10),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      ///作者
                      Text(
                        articleItem.shareUser.isEmpty
                            ? articleItem.author
                            : articleItem.shareUser,
                        style: TextStyle(fontSize: 14, color: Colors.grey),
                      ),

                      ///第一行 右侧 时间
                      Expanded(
                        child: Text(
                          articleItem.niceDate,
                          textAlign: TextAlign.right,
                          style: TextStyle(fontSize: 14, color: Colors.grey),
                        ),
                      )
                    ],
                  ),
                  Padding(padding: EdgeInsets.only(top: 5)),

                  ///中间的标题
                  Text(articleItem.title,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: TextStyle(
                          fontSize: 17,
                          color: Theme.of(context).textTheme.headline1.color,
                          fontWeight: FontWeight.bold)),
                  Padding(padding: EdgeInsets.only(top: 5)),
                  Text(
                    articleItem.desc,
                    overflow: TextOverflow.ellipsis,
                    maxLines: 2,
                    style: TextStyle(
                        fontSize: 13,
                        color: Theme.of(context).textTheme.bodyText1.color),
                  ),
                  Padding(padding: EdgeInsets.only(top: 8)),

                  ///底部的文章类型
                  Text(
                    "${articleItem.superChapterName}/${articleItem.chapterName}",
                    style: TextStyle(fontSize: 12, color: Colors.grey),
                  )
                ],
              ),
            )
          ],
        ),
        onTap: (){
          Get.toNamed(RoutesConfig.WEB_PAGE);
        },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
